package com.example.bijidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bijidemo.common.CustomException;
import com.example.bijidemo.common.R;
import com.example.bijidemo.common.SysLog;
import com.example.bijidemo.dto.RenwuDto;
import com.example.bijidemo.entity.Category;
import com.example.bijidemo.entity.Renwu;
import com.example.bijidemo.entity.RenwuJincheng;
import com.example.bijidemo.entity.RenwuWeidu;
import com.example.bijidemo.service.CategoryService;
import com.example.bijidemo.service.RenWuService;
import com.example.bijidemo.service.RenwuJinchengService;
import com.example.bijidemo.service.RenwuWeiduService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class RenwuController {
    @Autowired
    RenwuWeiduService renwuWeiduService;
    @Autowired
    RenWuService renWuService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    RenwuJinchengService renwuJinchengService;

    /**
     * 新增一个菜品
     */

    @PostMapping
    @SysLog("新增一个任务的操作")
    public R<String> save(@RequestBody RenwuDto renwuDto) throws ParseException {

        log.info(renwuDto.toString());

        renWuService.zengjia(renwuDto);

        return R.success("新增菜品成功");
    }


    @GetMapping("/page")
    @SysLog("分页查询任务，以及根据任务名查询")
    public R<Page> page(int page, int pageSize, String name) {
        //构造分页构造器
        Page<Renwu> pageInfo = new Page<>(page, pageSize);
        Page<RenwuDto> renwuDtoPage = new Page<>();
        //构造查询构造器
        LambdaQueryWrapper<Renwu> a = new LambdaQueryWrapper<>();
        //a.like(name != null, Renwu::getName, name);
        a.orderByDesc(Renwu::getUpdateTime);
        //开始查询
        renWuService.page(pageInfo, a);

        //对象的拷贝
        BeanUtils.copyProperties(pageInfo, renwuDtoPage, "records");
        List<Renwu> records = pageInfo.getRecords();
        List<RenwuDto> list = records.stream().map((item) -> {
            RenwuDto renwuDto = new RenwuDto();
            BeanUtils.copyProperties(item, renwuDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {

                String categoryName = category.getName();
                renwuDto.setCategoryName(categoryName);
            }
            Long id = item.getId();
            LambdaQueryWrapper<RenwuJincheng> aq=new LambdaQueryWrapper<>();
            aq.eq(RenwuJincheng::getRenwuId,id);
            RenwuJincheng renwuJincheng = renwuJinchengService.getOne(aq);
            if (renwuJincheng!=null){
                String value = renwuJincheng.getValue();
                renwuDto.setValue(value);
            }

            return renwuDto;

        }).collect(Collectors.toList());
        if (name!=null){
            List<RenwuDto> collect = list.stream().filter(s -> s.getName().contains(name)).collect(Collectors.toList());
            renwuDtoPage.setRecords(collect);
            return R.success(renwuDtoPage);
        }


        renwuDtoPage.setRecords(list);
        return R.success(renwuDtoPage);
    }
//查询项目的分页

    @GetMapping("/page2")
    @SysLog("根据项目的名称分页查询任务")
    public R<Page> page2(int page,int pageSize,String categoryName){
        //构造分页构造器
        Page<Renwu> pageInfo = new Page<>(page, pageSize);
        Page<RenwuDto> renwuDtoPage = new Page<>();
        //构造查询构造器
        LambdaQueryWrapper<Renwu> a = new LambdaQueryWrapper<>();
        //a.like(name != null, Renwu::getName, name);
        a.orderByDesc(Renwu::getUpdateTime);
        //开始查询
        renWuService.page(pageInfo, a);

        //对象的拷贝
        BeanUtils.copyProperties(pageInfo, renwuDtoPage, "records");
        List<Renwu> records = pageInfo.getRecords();
        List<RenwuDto> list = records.stream().map((item) -> {
            RenwuDto renwuDto = new RenwuDto();
            BeanUtils.copyProperties(item, renwuDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {

                String categoryName1 = category.getName();
                renwuDto.setCategoryName(categoryName1);
            }
            Long id = item.getId();
            LambdaQueryWrapper<RenwuJincheng> aq=new LambdaQueryWrapper<>();
            aq.eq(RenwuJincheng::getRenwuId,id);
            RenwuJincheng renwuJincheng = renwuJinchengService.getOne(aq);
            if (renwuJincheng!=null){
                String value = renwuJincheng.getValue();
                renwuDto.setValue(value);
            }

            return renwuDto;

        }).collect(Collectors.toList());
        if (categoryName!=null){
            List<RenwuDto> collect = list.stream().filter(s -> s.getCategoryName().contains(categoryName)).collect(Collectors.toList());
            renwuDtoPage.setRecords(collect);
            return R.success(renwuDtoPage);
        }


        renwuDtoPage.setRecords(list);
        return R.success(renwuDtoPage);

    }

    //查询负责人分页

    @GetMapping("/page3")
    @SysLog("根据负责人分页查询任务")
    public R<Page> page3(int page,int pageSize,String fuzeren){
        //构造分页构造器
        Page<Renwu> pageInfo = new Page<>(page, pageSize);
        Page<RenwuDto> renwuDtoPage = new Page<>();
        //构造查询构造器
        LambdaQueryWrapper<Renwu> a = new LambdaQueryWrapper<>();
        //a.like(name != null, Renwu::getName, name);
        a.orderByDesc(Renwu::getUpdateTime);
        //开始查询
        renWuService.page(pageInfo, a);

        //对象的拷贝
        BeanUtils.copyProperties(pageInfo, renwuDtoPage, "records");
        List<Renwu> records = pageInfo.getRecords();
        List<RenwuDto> list = records.stream().map((item) -> {
            RenwuDto renwuDto = new RenwuDto();
            BeanUtils.copyProperties(item, renwuDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {

                String categoryName1 = category.getName();
                renwuDto.setCategoryName(categoryName1);
            }
            Long id = item.getId();
            LambdaQueryWrapper<RenwuJincheng> aq=new LambdaQueryWrapper<>();
            aq.eq(RenwuJincheng::getRenwuId,id);
            RenwuJincheng renwuJincheng = renwuJinchengService.getOne(aq);
            if (renwuJincheng!=null){
                String value = renwuJincheng.getValue();
                renwuDto.setValue(value);
            }

            return renwuDto;

        }).collect(Collectors.toList());
        if (fuzeren!=null){
            List<RenwuDto> collect = list.stream().filter(s -> s.getFuzeren().contains(fuzeren)).collect(Collectors.toList());
            renwuDtoPage.setRecords(collect);
            return R.success(renwuDtoPage);
        }


        renwuDtoPage.setRecords(list);
        return R.success(renwuDtoPage);

    }


    //查询指派人的分页
    @GetMapping("/page4")
    @SysLog("根据指派人分页查询任务的操作")
    public R<Page> page4(int page,int pageSize,String zhipairen){
        //构造分页构造器
        Page<Renwu> pageInfo = new Page<>(page, pageSize);
        Page<RenwuDto> renwuDtoPage = new Page<>();
        //构造查询构造器
        LambdaQueryWrapper<Renwu> a = new LambdaQueryWrapper<>();
        //a.like(name != null, Renwu::getName, name);
        a.orderByDesc(Renwu::getUpdateTime);
        //开始查询
        renWuService.page(pageInfo, a);

        //对象的拷贝
        BeanUtils.copyProperties(pageInfo, renwuDtoPage, "records");
        List<Renwu> records = pageInfo.getRecords();
        List<RenwuDto> list = records.stream().map((item) -> {
            RenwuDto renwuDto = new RenwuDto();
            BeanUtils.copyProperties(item, renwuDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {

                String categoryName1 = category.getName();
                renwuDto.setCategoryName(categoryName1);
            }
            Long id = item.getId();
            LambdaQueryWrapper<RenwuJincheng> aq=new LambdaQueryWrapper<>();
            aq.eq(RenwuJincheng::getRenwuId,id);
            RenwuJincheng renwuJincheng = renwuJinchengService.getOne(aq);
            if (renwuJincheng!=null){
                String value = renwuJincheng.getValue();
                renwuDto.setValue(value);
            }

            return renwuDto;

        }).collect(Collectors.toList());
        if (zhipairen!=null){
            List<RenwuDto> collect = list.stream().filter(s -> s.getZhipairen().contains(zhipairen)).collect(Collectors.toList());
            renwuDtoPage.setRecords(collect);
            return R.success(renwuDtoPage);
        }


        renwuDtoPage.setRecords(list);
        return R.success(renwuDtoPage);

    }

    //查询优先级的分页
    @GetMapping("/page5")
    @SysLog("根据优先级分页查询的操作")
    public R<Page> page5(int page,int pageSize,String youxianji){
        //构造分页构造器
        Page<Renwu> pageInfo = new Page<>(page, pageSize);
        Page<RenwuDto> renwuDtoPage = new Page<>();
        //构造查询构造器
        LambdaQueryWrapper<Renwu> a = new LambdaQueryWrapper<>();
        //a.like(name != null, Renwu::getName, name);
        a.orderByDesc(Renwu::getUpdateTime);
        //开始查询
        renWuService.page(pageInfo, a);

        //对象的拷贝
        BeanUtils.copyProperties(pageInfo, renwuDtoPage, "records");
        List<Renwu> records = pageInfo.getRecords();
        List<RenwuDto> list = records.stream().map((item) -> {
            RenwuDto renwuDto = new RenwuDto();
            BeanUtils.copyProperties(item, renwuDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {

                String categoryName1 = category.getName();
                renwuDto.setCategoryName(categoryName1);
            }
            Long id = item.getId();
            LambdaQueryWrapper<RenwuJincheng> aq=new LambdaQueryWrapper<>();
            aq.eq(RenwuJincheng::getRenwuId,id);
            RenwuJincheng renwuJincheng = renwuJinchengService.getOne(aq);
            if (renwuJincheng!=null){
                String value = renwuJincheng.getValue();
                renwuDto.setValue(value);
            }

            return renwuDto;

        }).collect(Collectors.toList());
        if (youxianji!=null){
            List<RenwuDto> collect = list.stream().filter(s -> s.getYouxianji().contains(youxianji)).collect(Collectors.toList());
            renwuDtoPage.setRecords(collect);
            return R.success(renwuDtoPage);
        }


        renwuDtoPage.setRecords(list);
        return R.success(renwuDtoPage);

    }

    //查询项目的分页
    @GetMapping("/page6")
    @SysLog("根据完成度查询任务分页的操作")
    public R<Page> page6(int page,int pageSize,String valuess){
        //构造分页构造器
        Page<Renwu> pageInfo = new Page<>(page, pageSize);
        Page<RenwuDto> renwuDtoPage = new Page<>();
        //构造查询构造器
        LambdaQueryWrapper<Renwu> a = new LambdaQueryWrapper<>();
        //a.like(name != null, Renwu::getName, name);
        a.orderByDesc(Renwu::getUpdateTime);
        //开始查询
        renWuService.page(pageInfo, a);

        //对象的拷贝
        BeanUtils.copyProperties(pageInfo, renwuDtoPage, "records");
        List<Renwu> records = pageInfo.getRecords();
        List<RenwuDto> list = records.stream().map((item) -> {
            RenwuDto renwuDto = new RenwuDto();
            BeanUtils.copyProperties(item, renwuDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {

                String categoryName1 = category.getName();
                renwuDto.setCategoryName(categoryName1);
            }
            Long id = item.getId();
            LambdaQueryWrapper<RenwuJincheng> aq=new LambdaQueryWrapper<>();
            aq.eq(RenwuJincheng::getRenwuId,id);
            RenwuJincheng renwuJincheng = renwuJinchengService.getOne(aq);
            if (renwuJincheng!=null){
                String value = renwuJincheng.getValue();
                renwuDto.setValue(value);
            }

            return renwuDto;

        }).collect(Collectors.toList());
        if (valuess!=null){
            List<RenwuDto> collect = list.stream().filter(s -> s.getValue() !=null && s.getValue().contains(valuess)).collect(Collectors.toList());
            renwuDtoPage.setRecords(collect);
            return R.success(renwuDtoPage);
        }


        renwuDtoPage.setRecords(list);
        return R.success(renwuDtoPage);

    }

    //查询一个任务
    @GetMapping("/{id}")
    public R<RenwuDto> getbyidwithWeidu(@PathVariable Long id){
        RenwuDto a= renWuService.getbyidwith(id);
        return R.success(a);

    }

    //修改一个任务

    @PutMapping
    public R<String> update(@RequestBody RenwuDto renwuDto){

        renWuService.updatebywithrenwu(renwuDto);
        return R.success("修改成功");
    }

    /**
     * 任务的开启和关闭状态
     * 开启 状态
     */

    @PostMapping("/status/1")
    @SysLog("任务状态开启的操作")
    public R<String> upStatus1(@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Renwu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Renwu::getStatus,"1");
        updateWrapper.in(Renwu::getId,ids);
        renWuService.update(updateWrapper);
        return R.success("开启成功");
    }


    @PostMapping("/status/0")
    @SysLog("任务关闭的操作")
    public R<String> upStatus0(@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Renwu> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.set(Renwu::getStatus,"0");
        updateWrapper.in(Renwu::getId,ids);
        renWuService.update(updateWrapper);
        return R.success("关闭成功");
    }


    @DeleteMapping
    @SysLog("删除或批量删除任务的操作")
    public R<String> delete(@RequestParam List<Long> ids){

        log.info(String.valueOf(ids));
         renWuService.deletet(ids);


        return R.success("删除成功");
    }

    @GetMapping
    public R<List<Renwu>> getrenwu(){
        List<Renwu> reList = renWuService.getReList();
        log.info(reList.toString());
        return R.success(reList);

    }


}
